<template>
  <el-card class="list-page">
    <div class="list-h">
      <i></i>
      <el-form
        :inline="true"
        class="demo-form-inline"
        :model="formSearch"
        ref="formRef"
        @submit.prevent
      >
        <el-form-item label="单据名称" prop="billNameLike">
          <el-input
            clearable
            class="width-custom120"
            v-model.trim="formSearch.billNameLike"
          ></el-input>
        </el-form-item>
        <el-form-item label="单据编号" prop="billNumLike">
          <el-input
            clearable
            class="width-custom120"
            v-model.trim="formSearch.billNumLike"
          ></el-input>
        </el-form-item>
        <el-form-item label="所属项目" prop="projectNameLike">
          <el-input
            clearable
            class="width-custom120"
            v-model.trim="formSearch.projectNameLike"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button @click="handleReset">重置</el-button>
          <ReButton
            type="primary"
            debounce
            auto-loading
            v-auth="`/approval/toRead--query`"
            @click="handleMainSearch"
            >查询</ReButton
          >
        </el-form-item>
      </el-form>
    </div>
    <el-skeleton
      style="height: calc(100vh - 300px)"
      :rows="8"
      :loading="loading"
      animated
    >
    </el-skeleton>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      :loading="loading"
      v-show="!loading"
      :height="'calc(100vh - 300px)'"
      :header-cell-style="{
        background: '#F7F9FD',
        color: '#000',
        height: '48px',
        borderTop: '1px soild #E4E8F2'
      }"
    >
      <template #empty>
        <el-empty :image-size="150" description="暂无数据"></el-empty>
      </template>
      <el-table-column type="index" label="序号" width="50">
        <template v-slot="scope">
          <span>{{
            (formInline.pageNum - 1) * formInline.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="billName" label="单据名称"> </el-table-column>
      <el-table-column prop="billNum" label="单据编号"> </el-table-column>
      <el-table-column prop="projectName" label="所属项目"> </el-table-column>
      <el-table-column prop="starterName" label="提交人"> </el-table-column>
      <el-table-column prop="lastUpdateTime" label="到达时间" width="160">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="80">
        <template #default="{ row }">
          <el-button
            type="text"
            size="small"
            @click="handleDetail(row)"
            v-auth="`/approval/toRead--edit`"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <re-pagination
      v-if="total > 0"
      :total="total"
      v-model:page="formInline.pageNum"
      v-model:limit="formInline.pageSize"
      @pagination="handleSearch"
    />
  </el-card>
</template>

<script lang="ts">
import { reactive, ref, defineComponent } from "vue";
import ReButton from "/@/components/ReButton/index.vue";
import { useRouter } from "vue-router";
import { flowQueryNotify } from "/@/api/work";
import RePagination from "/@/components/RePagination/index.vue";
import { storageLocal } from "/@/utils/storage";
import { equipType } from "../types";
export default defineComponent({
  components: { ReButton, RePagination },
  setup() {
    const formRef = ref<HTMLFormElement | null>(null) as any;
    let formSearch = reactive({
      accepter: storageLocal.getItem("info")?.userId,
      feedback: "N",
      billNameLike: "",
      billNumLike: "",
      projectNameLike: ""
    });
    const router = useRouter();
    let loading = ref<boolean>(false);
    let tableData = ref([]);
    let total = ref<number>(0);
    let formInline = reactive({
      pageNum: 1,
      pageSize: 10
    });

    const getTableData = async (obj: Object = {}) => {
      loading.value = true;
      const { code, data } = await flowQueryNotify(
        Object.assign(formSearch, obj)
      );
      loading.value = false;
      if (code === 200) {
        tableData.value = data.list;
        total.value = data.rows;
      }
    };
    getTableData(formInline);
    const handleReset = (): void => {
      formRef.value.resetFields();
    };
    const handleSearch = () => {
      getTableData(formInline);
    };

    const handleMainSearch = done => {
      handleSearch();
      setTimeout(() => {
        done();
      }, 1000);
    };
    const handleDetail = row => {
      const {
        contractId,
        // nodeId,
        instanceId,
        flowId,
        preNodeId,
        billType,
        flowBaseId,
        nodeStatusCode,
        procInstId,
        taskId,
        billNum = ""
      } = row;
      let pathUrl = "/approval/detail";
      let pathQuery = {
        mode: "read",
        contractId,
        nodeDefId: flowBaseId ? nodeStatusCode.toUpperCase() : "ACT_010",
        instanceId: instanceId || procInstId,
        flowId,
        source: "work",
        back: "toRead",
        approvalType: "notify",
        readId: row.id,
        preNodeId,
        taskId,
        billNum,
        flowType: billType
      };

      if (billType && ["5", "6"].includes(billType)) {
        pathUrl = "/projectsA/acceptance/concealed/detail";
        pathQuery = Object.assign(pathQuery, {
          flowMinType: billType,
          contractId: flowBaseId
        });
      } else if (billType && ["7", "8"].includes(billType)) {
        pathUrl = "/projectsA/acceptance/completed/detail";
        pathQuery = Object.assign(pathQuery, {
          flowMinType: billType,
          contractId: flowBaseId
        });
      } else if (billType && ["9", "10"].includes(billType)) {
        pathUrl = "/projectsA/acceptance/material/detail";
        pathQuery = Object.assign(pathQuery, {
          flowMinType: billType,
          contractId: flowBaseId
        });
      } else if (billType && ["11", "15"].includes(billType)) {
        pathUrl = "/projectsA/quality/inspection/detail";
        pathQuery = Object.assign(pathQuery, {
          flowMinType: billType,
          contractId: flowBaseId
        });
      } else if (billType && equipType.includes(billType)) {
        pathUrl = "/approval/detail";
        pathQuery = Object.assign(pathQuery, {
          taskId: row.taskId,
          contractId: flowBaseId
        });
      }

      switch (billType) {
        case "1015":
          pathUrl = "/approval/construction";
          pathQuery = Object.assign(pathQuery, {
            taskId: row.taskId,
            contractId: flowBaseId,
            billName: row.billName,
            flowType: row.billType
          });
          break;
        case "1016":
        case "1017":
          pathUrl = "/approval/detail";
          pathQuery = Object.assign(pathQuery, {
            taskId: row.taskId,
            contractId: flowBaseId,
            flowType: row.billType,
            module: "material"
          });
          break;
        case "1018":
        case "1019":
          // 隐蔽验收
          pathUrl = "/approval/detail";
          pathQuery = Object.assign(pathQuery, {
            taskId: row.taskId,
            contractId: flowBaseId,
            billName: row.billName,
            flowType: row.billType,
            module: "conceal"
          });
          break;
        case "1020":
        case "1021":
          // 施工试验&见证检测
          pathUrl = "/approval/detail";
          pathQuery = Object.assign(pathQuery, {
            taskId: row.taskId,
            contractId: flowBaseId,
            billName: row.billName,
            flowType: row.billType,
            module: "witness"
          });
          break;
        case "1022":
          // 其他问题（随手拍）
          pathUrl = "/approval/detail";
          pathQuery = Object.assign(pathQuery, {
            taskId: row.taskId,
            contractId: flowBaseId,
            billName: row.billName,
            flowType: row.billType,
            module: "snap"
          });
          break;
        case "1032":
          // 车辆出入
          pathUrl = "/approval/detail";
          pathQuery = Object.assign(pathQuery, {
            taskId: row.taskId,
            contractId: flowBaseId,
            billName: row.billName,
            flowType: row.billType,
            module: "vehicle"
          });
          break;
        case "1030":
          // 安全文明
          pathUrl = "/approval/construction";
          pathQuery = Object.assign(pathQuery, {
            taskId: row.taskId,
            contractId: flowBaseId,
            billName: row.billName,
            flowType: row.billType,
            module: "civiliz"
          });
          break;
        default:
          break;
      }
      if (
        Number(billType) >= 1031 &&
        Number(billType) <= 1148 &&
        Number(billType) !== 1032
      ) {
        pathUrl = "/approval/detail";
        pathQuery = Object.assign(pathQuery, {
          taskId: row.taskId,
          contractId: flowBaseId,
          billName: row.billName,
          flowType: row.billType,
          module: Number(billType) == 1031 ? "inspection" : ""
        });
      }
      router.replace({
        path: pathUrl,
        query: pathQuery
      });
    };

    return {
      formRef,
      formSearch,
      loading,
      formInline,
      total,
      tableData,
      handleReset,
      handleSearch,
      handleMainSearch,
      handleDetail
    };
  }
});
</script>

<style lang="scss" scoped></style>
